package com.xu.common.widget.recyclerview;

import android.support.annotation.LayoutRes;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.xu.common.R;
import com.xu.common.widget.recyclerview.interfacebind.IAdapterCallBack;
import com.xu.common.widget.recyclerview.interfacebind.IAdapterListen;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by 12708 on 2018/5/27.
 */

public abstract class RecyclerAdapter<Data> extends RecyclerView.Adapter
        <RecyclerAdapter.ViewHolder<Data>> implements View.OnClickListener,
        View.OnLongClickListener, IAdapterCallBack {

    final List<Data> mDatas;
    //监听接口
    IAdapterListen iAdapterListen = null;

    /**
     * 三种构造器
     */
    public RecyclerAdapter() {
        this(null);
    }
    public RecyclerAdapter(IAdapterListen<Data> iAdapterListen) {
        this(new ArrayList<Data>(),iAdapterListen);
    }
    public RecyclerAdapter(List<Data> mDatas, IAdapterListen<Data> iAdapterListen) {
        this.mDatas = mDatas;
        this.iAdapterListen = iAdapterListen;
    }

    /**
     * 复写返回布局的类型
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        return getItemViewType(position, mDatas.get(position));
    }
    /**
     * @param postion
     * @param mData
     * @return
     */
    @LayoutRes
    protected abstract int getItemViewType(int postion, Data mData);



    /**
     * 创建一个ViewHoder（界面创建）
     *
     * @param parent   RecyclerView
     * @param viewType 界面的类型，约定xml布局的Id
     * @return viewHolder
     */
    @Override
    public ViewHolder<Data> onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View root = inflater.inflate(viewType, parent, false);
        ViewHolder<Data> holder = onCreateView(root, viewType);
        root.setTag(R.id.tag_recycler_holder, holder);
        root.setOnClickListener(this);
        root.setOnLongClickListener(this);
        //进行界面绑定
        holder.unbinder = ButterKnife.bind(holder, root);
        //绑定hoder
        holder.iAdapterCallBack = this;
        return holder;
    }

    @Override
    public void onClick(View v) {
        RecyclerAdapter.ViewHolder holder = (RecyclerAdapter.ViewHolder) v.getTag(R.id.tag_recycler_holder);
        if (iAdapterListen != null) {
            //获取当前数据坐标
            int postion = holder.getAdapterPosition();
            //获取当前数据
            Data data = mDatas.get(postion);
            iAdapterListen.update(holder, data);
        }
    }


    @Override
    public boolean onLongClick(View v) {
        RecyclerAdapter.ViewHolder holder = (RecyclerAdapter.ViewHolder) v.getTag(R.id.tag_recycler_holder);
        if (iAdapterListen != null) {
            //获取当前数据坐标
            int postion = holder.getAdapterPosition();
            //获取当前数据
            Data data = mDatas.get(postion);
            iAdapterListen.update(holder, data);
            return true;
        }
        return false;
    }


    /**
     * Hoder绑定数据
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(ViewHolder<Data> holder, int position) {
        Data data = mDatas.get(position);
        holder.bind(data);
    }

    //传入布局与类型
    protected abstract ViewHolder<Data> onCreateView(View root, int type);


    //得到Item数量
    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    //点击事件的监听方法
    public void setListen(IAdapterListen adapterListen) {
        this.iAdapterListen = adapterListen;
    }

    /**
     * @param data 增加单个数据
     */
    public void add(Data data) {
        mDatas.add(data);
        notifyItemInserted(mDatas.size() - 1);

    }

    /**
     * @param mdata 增加数据集合,并通知更新
     */
    public void add(Data... mdata) {
        if (mdata != null & mdata.length > 0) {
            int startInsert = mDatas.size();
            Collections.addAll(mDatas, mdata);
            notifyItemRangeChanged(startInsert, mdata.length);
        }
    }

    /**
     * @param mdata 增加一堆数据,并通知更新
     */
    public void add(Collection<Data> mdata) {
        if (mdata != null & mdata.size() > 0) {
            int startInsert = mDatas.size();
            mDatas.addAll(mdata);
            notifyItemRangeChanged(startInsert, mdata.size());
        }
    }

    /**
     * 清除操作
     */
    public void clear() {
        if (mDatas != null & mDatas.size() > 0) {
            mDatas.clear();
            notifyDataSetChanged();
        }
    }

    /**
     * holder性能优化的作用
     *
     * @param <Data>
     */
    public abstract static class ViewHolder<Data> extends RecyclerView.ViewHolder {
        private Unbinder unbinder = null;
        protected Data mData;
        private IAdapterCallBack<Data> iAdapterCallBack = null;

        public ViewHolder(View itemView) {
            super(itemView);
        }

        /**
         * 用于绑定数据的触发
         *
         * @param data
         */
        void bind(Data data) {
            this.mData = data;
            //绑定数据事件
            onBind(data);
        }

        /**
         * 当触发绑定数据的时候，必须覆写
         *
         * @param data
         */
        protected abstract void onBind(Data data);

        public void updataData(Data data) {

            if (iAdapterCallBack != null) {
                iAdapterCallBack.update(data, this);
            }

        }
    }
}
